package logic

import (
	"context"
	"fmt"
	"yk/models/mysql"

	"yk/userrpc/internal/svc"
	"yk/userrpc/userrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type LoginLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic {
	return &LoginLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *LoginLogic) Login(in *userrpc.LoginRequest) (*userrpc.LoginResponse, error) {
	// todo: add your logic here and delete this line
	password := in.Password
	username := in.Username
	var users mysql.User
	mysql.DB.Debug().Where("username =?", username).Find(&users)
	if users.ID == 0 {
		return nil, fmt.Errorf("当前账号以存在,请前去登陆")
	}
	if users.Password != password {
		return nil, fmt.Errorf("密码错误,钱 重新输入")
	}
	return &userrpc.LoginResponse{
		Id: int64(users.ID),
	}, nil
}
